package com.example.task1.ui.user;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.task1.MainActivity;
import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.User;
import com.example.task1.bean.UserInfo;
import com.example.task1.http.Api;
import com.example.task1.ui.user.activity.UserInfoActivity;
import com.example.task1.ui.user.adapter.UserRvAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link UserFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class UserFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ImageView personImage;
    private TextView userName;
    private RecyclerView rvList;
    private Button btnBack;


    public UserFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment UserFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static UserFragment newInstance(String param1, String param2) {
        UserFragment fragment = new UserFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
        btnBack.setOnClickListener(v -> {
            MyApplication.TOKEN = "";
            MyApplication.user = null;
            SharedPreferences.Editor edit = MyApplication.getSharedPreferences().edit();
            edit.remove("token");
            edit.commit();
            MyApplication.intent(getContext(), MainActivity.class);
        });
    }

    private void initData() {
        List<User> users = Arrays.asList(new User("个人信息", R.drawable.ic_baseline_account_circle_24)
                , new User("订单列表", R.drawable.ic_baseline_order_24)
                , new User("修改密码", R.drawable.ic_baseline_pwd_24)
                , new User("意见反馈", R.drawable.ic_baseline_feedback_24));
        UserRvAdapter userRvAdapter = new UserRvAdapter(getContext());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setSmoothScrollbarEnabled(false);
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(userRvAdapter);
        userRvAdapter.setDate(users);
    }


    private void initView(View view) {
        userName = view.findViewById(R.id.user_name);
        rvList = view.findViewById(R.id.rv_list);
        btnBack = view.findViewById(R.id.btn_back);
        personImage = view.findViewById(R.id.personImage);
        userName.setText(MyApplication.user.getNickName() == null ? MyApplication.user.getUserName() : MyApplication.user.getNickName());
        if (!TextUtils.isEmpty(MyApplication.user.getAvatar())) {
            MyApplication.glide(personImage, MyApplication.user.getAvatar());
        }
    }
}